﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace EntityDA
{
    public class ChiTietHoaDonDB
    {
        //lấy tất cả chi tiết hóa đơn của 1 hóa đơn
        public List<ChiTietHoaDonData> GetAllCTHD(int MaHD)
        {
            List<ChiTietHoaDonData> DSCTHD = new List<ChiTietHoaDonData>();
            ConnectionDB cn = new ConnectionDB();
            string sql = "";
            sql = "select * from CTHD where MaHD=@MaHD";
            cn.cmd.Parameters.Add(new SqlParameter("@MaHD", MaHD));
            cn.cmd.CommandText = sql;
            SqlDataReader reader = cn.cmd.ExecuteReader();
            ChiTietHoaDonData CTHD = null;
            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    CTHD = new ChiTietHoaDonData();
                    if (!reader.IsDBNull(0))
                        CTHD.MaHD = reader.GetInt32(0);
                    if (!reader.IsDBNull(1))
                        CTHD.MaSach = reader.GetInt32(1);
                    if (!reader.IsDBNull(2))
                        CTHD.SL = reader.GetInt32(2);
                    if (!reader.IsDBNull(3))
                        CTHD.DonGia = reader.GetDecimal(3);
                    if (!reader.IsDBNull(4))
                        CTHD.ThanhTien = reader.GetDecimal(4);
                    if (!reader.IsDBNull(5))
                        CTHD.XuLy = reader.GetInt32(5);
                    DSCTHD.Add(CTHD);
                }
            }
            return DSCTHD;
        }

        //thêm 1 chi tiết hoá đơn
        public bool ThemCTHD(ChiTietHoaDonData CTHD)
        {
            try
            {
                ConnectionDB cn = new ConnectionDB();
                string sql = "insert into CTHD(MaHD,MaSach,SL,DonGia)";
                sql += "values(@MaHD,@MaSach,@SL,@DonGia)";
                cn.cmd.CommandText = sql;
                cn.cmd.Parameters.Add(new SqlParameter("@MaHD", CTHD.MaHD));
                cn.cmd.Parameters.Add(new SqlParameter("@MaSach", CTHD.MaSach));
                cn.cmd.Parameters.Add(new SqlParameter("@SL", CTHD.SL));
                cn.cmd.Parameters.Add(new SqlParameter("@DonGia", CTHD.DonGia));
                cn.cmd.CommandText = sql;
                cn.cmd.ExecuteNonQuery();
            }
            catch
            {
                return false;
            }
            return true;
        }
    }
}